package com.zhenxiang.config;

import com.alibaba.fastjson.JSON;
import com.zhenxiang.interceptor.SignAuthInterceptor;
import com.zhenxiang.properties.OpenSignInterceptorProperties;
import com.zhenxiang.properties.OpenSignProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;

/**
 * @Author 真香
 * @Date 2021/4/20 16:30
 * @Version 1.0
 */

@Slf4j
@Configuration
public class OpenSignWebMvcConfig  implements WebMvcConfigurer {

    @Autowired
    private SignAuthInterceptor signAuthInterceptor;

    @Autowired
    private OpenSignProperties openSignProperties;

    // 拦截器配置
    private OpenSignInterceptorProperties interceptorConfig;

    // 注入spring 容器
    @Bean
    public SignAuthInterceptor signAuthInterceptor () {
        return new SignAuthInterceptor();
    }

    @PostConstruct
    public void init () {
        interceptorConfig = openSignProperties.getInterceptor();
        log.debug("openSignProperties：{}", JSON.toJSONString(interceptorConfig));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        /**
         * 注册签名拦截器
         */
        if (interceptorConfig.getSign().isEnable()) {
            registry.addInterceptor(signAuthInterceptor())
                    .addPathPatterns(interceptorConfig.getSign().getIncludePaths())
                    .excludePathPatterns(interceptorConfig.getSign().getExcludePaths());
        }
    }
}
